package yl.hs.bmipfull.controllers.material;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.material.IRMWarehouseService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.math.BigDecimal;

/**
 * 作用:  仓库信息控制层<br/>
 * 创建时间: 2021年05月19日 11时02分59秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "仓库信息Web接口", tags = "采购管理——仓库信息操作")
@RequestMapping(value = "/api/purchase")
@RestController
public class RMWarehouseController extends ControllerBase {

    @Autowired
    IRMWarehouseService service;
    @Autowired
    ISearchViewService svSVC;

    @ApiOperation(value = "查询仓库信息", notes = "查询仓库信息")
    @RequestMapping(value = "/warehouse", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询仓库信息", notes = "查询仓库信息")
    @RequestMapping(value = "/selectPortWH/{rmSN}/{lineSN}", method = RequestMethod.GET)
    public WebResult queryPortWhList(
            @PathVariable String rmSN,
            @PathVariable(required = false, name = "lineSN") String lineSN) {
        try {
            return success("查询成功", service.queryPortList(rmSN, lineSN));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询仓库信息", notes = "查询仓库信息")
    @RequestMapping(value = "/warehouse/{rmCateSn}/{lineSn}", method = RequestMethod.GET)
    public WebResult queryWarehouse(
            @PathVariable String rmCateSn,
            @PathVariable String lineSn) {
        try {
            return success("查询成功", service.queryList(rmCateSn, lineSn,true));
        } catch (Exception e) {
            return error(e);
        }
    }


    @ApiOperation(value = "查询仓库信息", notes = "查询仓库信息")
    @RequestMapping(value = {"/selectWH/{type}/{pSN}/{lineSN}", "/selectWH/{type}/{pSN}"}, method = RequestMethod.GET)
    public WebResult querySelectList(
            @PathVariable short type,
            @PathVariable String pSN,
            @PathVariable(required = false, name = "lineSN") String lineSN) {
        try {
            return success("查询成功", service.queryList(type, pSN, lineSN));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "查询仓库信息", notes = "分页查询仓库信息")
    @RequestMapping(value = "/warehouse/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            svSVC.updateUserMenuView(query);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }


    @ApiOperation(value = "查询仓库信息", notes = "根据仓库信息编号查询仓库信息")
    @RequestMapping(value = "/warehouse/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存仓库信息", notes = "查询/更新仓库信息信息")
    @RequestMapping(value = "/warehouse", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveWarehouse(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }

    }

    @ApiOperation(value = "删除仓库信息信息", notes = "根据仓库信息编号删除系仓库信息信息")
    @RequestMapping(path = "/warehouse", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "仓库信息编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String[] sn) {
        var result=service.delete(sn);
        if ( result> 0) {
            return success("删除成功");
        } else if (result==-1){
            return fail("删除失败,仓库下具有原材对应信息的无法删除！");
        }else {
            return fail("删除失败");
        }

    }
    @ApiOperation(value = "清除罐中原材",notes = "清除罐中原材")
    @RequestMapping(path = "/warehouse/tankcleaning/{sn}",method = RequestMethod.GET)
    public WebResult tankCleaning(@ApiParam(value = "sn",name = "仓库编号") @PathVariable String sn){
        var warehouse = service.findBySn(sn);
        warehouse.setRmSN("");
        warehouse.setInventory(BigDecimal.valueOf(0));
        var result = service.update(warehouse);
        if(result>0) {
            return success("清除成功");
        };
        return fail("清除失败");
    }
    @ApiOperation(value = "恢复删除数据接口", notes = "根据仓库信息编号恢复系仓库信息信息")
    @RequestMapping(path = "/warehouse/changeStateBySn/{sn}/{state}", method = RequestMethod.GET)
    public WebResult changeStateBySn(@ApiParam(value = "仓库信息编号", name = "sn", allowMultiple = true, required = true) @PathVariable String[] sn,@ApiParam(value = "恢复数据到的状态",name = "state") @PathVariable short state) {
        if (service.recovery(sn,state) > 0) {
            return success("操作成功");
        } else {
            return fail("操作失败");
        }
    }
}